package com.xc.quickstart.common.model;

import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(description = "基础返回对象")
public class ApiResponse {

    public static final Integer OK_CODE = 200;
    public static final String OK_MSG = "success";

    public static final Integer FAIL_CODE = 400;
    public static final String FAIL_MSG = "fail";

    public static final Integer NO_LOGIN_CODE = 3001;
    public static final String NO_LOGIN_MSG = "no_login";

    public static ApiResponse INVALID_TOKEN = new ApiResponse(1002, "error", "登录已失效");

    public ApiResponse(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ApiResponse(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    @ApiModelProperty(value = "返回码")
    protected Integer code;
    @ApiModelProperty(value = "返回消息")
    protected String msg;

    @ApiModelProperty(value = "服务器时间（秒）")
    @JSONField(name = "server_time")
    protected Integer serverTime = (int) (System.currentTimeMillis() / 1000);
    @ApiModelProperty(value = "数据")
    protected Object data;

    public static final ApiResponse OK = new ApiResponse(OK_CODE, OK_MSG);

    public static final ApiResponse FAIL = new ApiResponse(FAIL_CODE, FAIL_MSG);

    public static final ApiResponse NO_LOGIN = new ApiResponse(NO_LOGIN_CODE, NO_LOGIN_MSG);

    public static ApiResponse noLogin() {
        return NO_LOGIN;
    }

    public static ApiResponse noLogin(String msg) {
        return new ApiResponse(NO_LOGIN_CODE, msg);
    }

    public static ApiResponse ok() {
        return OK;
    }

    public static ApiResponse ok(Object data) {
        return new ApiResponse(OK_CODE, OK_MSG, data);
    }

    public static ApiResponse ok(String msg, Object data) {
        return new ApiResponse(OK_CODE, msg, data);
    }

    public static ApiResponse fail() {
        return FAIL;
    }

    public static ApiResponse fail(String msg) {
        return new ApiResponse(FAIL_CODE, msg);
    }
}
